/**
 * @fileoverview Blocks for the Phaser Blockly modification.
 *
 * @license Copyright 2017 The Coding with Chrome Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @author mbordihn@google.com (Markus Bordihn)
 */

{namespace cwc.soy.phaser.Blocks autoescape="strict"}


/**
 * Blockly blocks.
 */
{template .toolbox}
  {@param? files: ?}

  <xml>
    <category name="@@BLOCKS_PHASER__GAME" colour="75">
      {call .gameBlocks /}
    </category>

    <category name="@@BLOCKS_PHASER__PRELOAD" colour="165">
      {call .preloadBlocks /}
    </category>

    <category name="@@BLOCKS_PHASER__CREATE" colour="30">

      {call .createBlocks /}

      {call .defaultPhaserBlocks}
        {param opt_show_create_blocks: true /}
        {param opt_hide_event_blocks: true /}
      {/call}

      <category name="@@BLOCKS_PHASER__GENERATOR" colour="105">
        {call cwc.soy.phaser.GeneratorBlocks.toolbox /}
      </category>

      <category name="@@BLOCKS_PHASER__WORLD" colour="345">
        {call .worldBlocks}
          {param opt_show_create_blocks: true /}
        {/call}
      </category>

    </category>

    <category name="@@BLOCKS_PHASER__INPUT" colour="135">

      {call cwc.soy.phaser.InputBlocks.toolbox /}

      {call .defaultPhaserBlocks}
        {param opt_hide_input_blocks: true /}
      {/call}

    </category>

    <category name="@@BLOCKS_PHASER__UPDATE" colour="315">

      {call .updateBlocks /}

      {call .defaultPhaserBlocks}
        {param opt_hide_event_blocks: true /}
      {/call}

      <category name="@@BLOCKS_PHASER__TIME" colour="350">
        {call .timeBlocks /}
      </category>

      <category name="@@BLOCKS_PHASER__GROUP" colour="260">
        {call cwc.soy.phaser.GroupBlocks.toolbox data="all" /}
      </category>

      <category name="@@BLOCKS_PHASER__WORLD" colour="345">
        {call .worldBlocks /}
      </category>

    </category>

    <category name="@@BLOCKS_PHASER__RENDERER" colour="195">
      {call .renderBlocks /}
    </category>

    <sep></sep>

    {if $files}
      <category name="@@BLOCKS_PHASER__FILES" colour="150">
        <button text="{msg desc=""}@@BLOCKS_PHASER__OPEN_FILE_LIBRARY{/msg}" callbackKey="FILE_LIBRARY_MANAGMENT" />
        {call .files data="all" /}
      </category>
    {/if}

    <category name="@@BLOCKS_PHASER__EXAMPLE_FILES" colour="150">
      {call .sampleFiles data="all" /}
    </category>

    <sep></sep>

    {call cwc.soy.Blocks.defaultBlocks /}

    <sep></sep>

    {call cwc.soy.Blocks.defaultAdvancedBlocks /}
  </xml>
{/template}


/**
 * Phaser files
 */
{template .files}
  {@param files: ?}

  {foreach $file in keys($files)}
    {switch $files[$file]['media_type']}
      {case 'audio'}
        <block type="phaser_load_audio">
          <field name="name">{$file}</field>
          <value name="audio">
            <block type="general_file_library_audio">
              <field name="filename">"{$file}"</field>
            </block>
          </value>
        </block>
      {case 'image'}
        <block type="phaser_load_image">
          <field name="name">{$file}</field>
          <value name="image">
            <block type="general_file_library_image">
              <field name="filename">"{$file}"</field>
              <field name="preview">{$files[$file]['content']}</field>
            </block>
          </value>
        </block>
      {case 'text'}
        <block type="general_file_library_text">
          <field name="filename">"{$file}"</field>
        </block>
      {default}
        <block type="general_file_library">
          <field name="filename">"{$file}"</field>
        </block>
    {/switch}
  {/foreach}
{/template}


/**
 * Phaser sample files
 */
{template .sampleFiles}
  <block type="phaser_load_image">
    <field name="name">50px_red</field>
      <value name="image">
        <block type="general_sample_image_50px_red"></block>
    </value>
  </block>

  <block type="phaser_load_image">
    <field name="name">50px_green</field>
      <value name="image">
        <block type="general_sample_image_50px_green"></block>
    </value>
  </block>

  <block type="phaser_load_image">
    <field name="name">50px_blue</field>
      <value name="image">
        <block type="general_sample_image_50px_blue"></block>
    </value>
  </block>

  <block type="phaser_load_image">
    <field name="name">ball_red</field>
      <value name="image">
        <block type="general_sample_image_ball_red"></block>
    </value>
  </block>

  <block type="phaser_load_image">
    <field name="name">ball_green</field>
      <value name="image">
        <block type="general_sample_image_ball_green"></block>
    </value>
  </block>

  <block type="phaser_load_image">
    <field name="name">ball_blue</field>
      <value name="image">
        <block type="general_sample_image_ball_blue"></block>
    </value>
  </block>

  <block type="phaser_load_image">
    <field name="name">paddle</field>
      <value name="image">
        <block type="general_sample_image_paddle"></block>
    </value>
  </block>

  <block type="phaser_load_image">
    <field name="name">player</field>
      <value name="image">
        <block type="general_sample_image_player"></block>
    </value>
  </block>

  <block type="phaser_load_image">
    <field name="name">bg_01</field>
      <value name="image">
        <block type="general_sample_image_bg_01"></block>
    </value>
  </block>

  <block type="phaser_load_image">
    <field name="name">bg_02</field>
      <value name="image">
        <block type="general_sample_image_bg_02"></block>
    </value>
  </block>
{/template}


/**
 * Default Phaser blocks.
 */
{template .defaultPhaserBlocks}
  {@param? opt_hide_event_blocks: bool}
  {@param? opt_hide_input_blocks: bool}
  {@param? opt_show_create_blocks: bool}

  <category name="@@BLOCKS_PHASER__AUDIO" colour="245">
    {call .audioBlocks data="all" /}
  </category>

  <category name="@@BLOCKS_PHASER__SPRITE" colour="225">
    {call .spriteBlocks data="all" /}
  </category>

  <category name="@@BLOCKS_PHASER__TILE_SPRITE" colour="285">
    {call .tileSpriteBlocks data="all" /}
  </category>

  <category name="@@BLOCKS_PHASER__PHYSICS_SPRITE" colour="0">
    {call cwc.soy.phaser.ArcadePhysicsBlocks.toolbox data="all" /}
  </category>

  <category name="@@BLOCKS_PHASER__TEXT" colour="255">
    {call .textBlocks data="all" /}
  </category>

  {if not $opt_hide_input_blocks}
    <category name="@@BLOCKS_PHASER__INPUTS" colour="255">
      {call cwc.soy.phaser.InputBlocks.toolbox data="all" /}
    </category>
  {/if}
{/template}


/**
 * Create blocks.
 */
{template .createBlocks}
  <block type="phaser_create"></block>

  <block type="phaser_stage_background_color"></block>

  <block type="phaser_add_background"></block>

  <block type="phaser_add_background_scaled"></block>

  <block type="phaser_group_add">
    <value name="variable">
      <block type="variables_get">
        <field name="VAR">obstacle_group</field>
      </block>
    </value>
  </block>

  <block type="phaser_time_loop_event">
    <value name="time">
      <block type="math_number"><field name="NUM">1500</field></block>
    </value>
  </block>
{/template}


/**
 * Preload blocks.
 */
{template .preloadBlocks}
  <block type="phaser_preload"></block>
  <block type="phaser_load_image"></block>
  <block type="phaser_load_audio"></block>
{/template}


/**
 * Render blocks.
 */
{template .renderBlocks}
  <block type="phaser_render"></block>

  <block type="phaser_debug_camera"></block>

  <block type="phaser_debug_pointer"></block>

  <block type="phaser_debug_sprite">
    <value name="variable">
      <block type="variables_get">
        <field name="VAR">sprite</field>
      </block>
    </value>
  </block>
{/template}


/**
 * World blocks.
 */
{template .worldBlocks}
  {@param? opt_show_create_blocks: bool}

  {if $opt_show_create_blocks}
    <block type="phaser_world_resize"></block>

    <block type="phaser_world_arcade_physics">
      <value name="value">
        <block type="math_number"><field name="NUM">0</field></block>
      </value>
    </block>

    <block type="phaser_world_sort_direction">
      <field name="property">1</field>
    </block>
  {/if}

  {if not $opt_show_create_blocks}
    <block type="phaser_world_wrap">
      <value name="variable">
        <block type="variables_get">
          <field name="VAR">sprite</field>
        </block>
      </value>
      <value name="value">
        <block type="math_number"></block>
      </value>
    </block>
  {/if}

  <block type="phaser_world_attributes"></block>
{/template}


/**
 * Audio blocks.
 */
{template .audioBlocks}
  {@param? opt_show_create_blocks: bool}

  {if $opt_show_create_blocks}
    <block type="phaser_audio_add_bgm">
      <value name="variable">
        <block type="variables_get">
          <field name="VAR">bgm</field>
        </block>
      </value>
    </block>

    <block type="phaser_audio_add">
      <value name="variable">
        <block type="variables_get">
          <field name="VAR">sound</field>
        </block>
      </value>
    </block>
  {/if}

  <block type="phaser_audio_play">
    <value name="variable">
      <block type="variables_get">
        <field name="VAR">sound</field>
      </block>
    </value>
  </block>
  <block type="phaser_audio_pause">
    <value name="variable">
      <block type="variables_get">
        <field name="VAR">sound</field>
      </block>
    </value>
  </block>
  <block type="phaser_audio_resume">
    <value name="variable">
      <block type="variables_get">
        <field name="VAR">sound</field>
      </block>
    </value>
  </block>
  <block type="phaser_audio_stop">
    <value name="variable">
      <block type="variables_get">
        <field name="VAR">sound</field>
      </block>
    </value>
  </block>
{/template}


/**
 * Update blocks.
 */
{template .updateBlocks}
  <block type="phaser_update"></block>
{/template}


/**
 * Text blocks.
 */
{template .textBlocks}
  {@param? opt_show_create_blocks: bool}
  {@param? opt_hide_event_blocks: bool}

  {if $opt_show_create_blocks}
    <block type="phaser_text_add">
      <value name="text">
        <block type="text">
          <field name="TEXT">Title</field>
        </block>
      </value>
      <value name="x">
        <block type="math_number"><field name="NUM">100</field></block>
      </value>
      <value name="y">
        <block type="math_number"><field name="NUM">10</field></block>
      </value>
    </block>

    <block type="phaser_text_add">
      <value name="text">
        <block type="text">
          <field name="TEXT">Hello World</field>
        </block>
      </value>
      <value name="x">
        <block type="math_number"><field name="NUM">10</field></block>
      </value>
      <value name="y">
        <block type="math_number"><field name="NUM">10</field></block>
      </value>
    </block>

    <block type="phaser_dynamic_text_highscore_add">
      <value name="variable">
        <block type="variables_get">
          <field name="VAR">highscore</field>
        </block>
      </value>
      <value name="score">
        <block type="math_number"><field name="NUM">0</field></block>
      </value>
      <value name="x">
        <block type="math_number"><field name="NUM">10</field></block>
      </value>
      <value name="y">
        <block type="math_number"><field name="NUM">10</field></block>
      </value>
    </block>

    <block type="phaser_dynamic_text_add">
      <value name="variable">
        <block type="variables_get">
          <field name="VAR">text</field>
        </block>
      </value>
      <value name="text">
        <block type="text">
          <field name="TEXT">Text</field>
        </block>
      </value>
      <value name="x">
        <block type="math_number"><field name="NUM">10</field></block>
      </value>
      <value name="y">
        <block type="math_number"><field name="NUM">10</field></block>
      </value>
    </block>

    <block type="phaser_action_text_add">
      <value name="variable">
        <block type="variables_get">
          <field name="VAR">start_text</field>
        </block>
      </value>
      <value name="text">
        <block type="text">
          <field name="TEXT">Start the Game!</field>
        </block>
      </value>
      <value name="x">
        <block type="math_number"><field name="NUM">10</field></block>
      </value>
      <value name="y">
        <block type="math_number"><field name="NUM">10</field></block>
      </value>
    </block>
  {/if}


  {if not $opt_hide_event_blocks or $opt_show_create_blocks}
    <block type="phaser_text_clicked">
      <value name="variable">
        <block type="variables_get">
          <field name="VAR">start_text</field>
        </block>
      </value>
    </block>
  {/if}


  <block type="phaser_text_change">
    <value name="variable">
      <block type="variables_get">
        <field name="VAR">text</field>
      </block>
    </value>
    <value name="text">
      <block type="text">
        <field name="TEXT">Hello World</field>
      </block>
    </value>
  </block>

  <block type="phaser_text_change">
    <value name="variable">
      <block type="variables_get">
        <field name="VAR">highscore</field>
      </block>
    </value>
    <value name="text">
      <block type="math_arithmetic">
        <field name="OP">ADD</field>
        <value name="A">
          <block type="phaser_text_get_number">
            <value name="variable">
              <block type="variables_get">
                <field name="VAR">highscore</field>
              </block>
            </value>
          </block>
        </value>
        <value name="B">
          <shadow type="math_number">
            <field name="NUM">1</field>
          </shadow>
        </value>
      </block>
    </value>
  </block>

  <block type="phaser_text_get">
    <value name="variable">
      <block type="variables_get">
        <field name="VAR">text</field>
      </block>
    </value>
  </block>

  <block type="phaser_text_get_number">
    <value name="variable">
      <block type="variables_get">
        <field name="VAR">highscore</field>
      </block>
    </value>
  </block>
{/template}


/**
 * Tile Sprite blocks.
 */
{template .tileSpriteBlocks}
  {@param? opt_show_create_blocks: bool}

  {if $opt_show_create_blocks}

    <block type="phaser_tile_sprite_background">
      <value name="x">
        <block type="math_number"><field name="NUM">-50</field></block>
      </value>
      <value name="y">
        <block type="math_number"><field name="NUM">0</field></block>
      </value>
    </block>

    <block type="phaser_tile_sprite_floor_add">
      <value name="variable">
        <block type="variables_get">
          <field name="VAR">floor</field>
        </block>
      </value>
      <value name="x">
        <block type="math_number"><field name="NUM">-50</field></block>
      </value>
      <value name="y">
        <block type="math_number"><field name="NUM">0</field></block>
      </value>
    </block>

    <block type="phaser_tile_sprite_ceiling_add">
      <value name="variable">
        <block type="variables_get">
          <field name="VAR">ceiling</field>
        </block>
      </value>
      <value name="x">
        <block type="math_number"><field name="NUM">-50</field></block>
      </value>
      <value name="y">
        <block type="math_number"><field name="NUM">0</field></block>
      </value>
    </block>

    <block type="phaser_tile_sprite_add">
      <value name="variable">
        <block type="variables_get">
          <field name="VAR">tile_sprite</field>
        </block>
      </value>
      <value name="x">
        <block type="math_number"><field name="NUM">0</field></block>
      </value>
      <value name="y">
        <block type="math_number"><field name="NUM">550</field></block>
      </value>
    </block>

    <block type="phaser_tile_sprite_crop">
      <value name="variable">
        <block type="variables_get">
          <field name="VAR">blocks</field>
        </block>
      </value>
    </block>
  {/if}

  <block type="phaser_tile_sprite_adjust">
    <value name="variable">
      <block type="variables_get">
        <field name="VAR">sprite</field>
      </block>
    </value>
    <value name="value">
      <block type="math_number"></block>
    </value>
  </block>

  <block type="phaser_tile_sprite_destroy">
    <value name="variable">
      <block type="variables_get">
        <field name="VAR">sprite</field>
      </block>
    </value>
  </block>

  <block type="phaser_tile_sprite_autoScroll">
    <value name="variable">
      <block type="variables_get">
        <field name="VAR">blocks</field>
      </block>
    </value>
    <value name="x">
      <block type="math_number"><field name="NUM">-50</field></block>
    </value>
    <value name="y">
      <block type="math_number"><field name="NUM">0</field></block>
    </value>
  </block>

  <block type="phaser_tile_sprite_immovable">
    <value name="variable">
      <block type="variables_get">
        <field name="VAR">blocks</field>
      </block>
    </value>
  </block>
{/template}


/**
 * Time blocks.
 */
{template .timeBlocks}
  <block type="phaser_time_event">
    <value name="time">
      <block type="math_number"><field name="NUM">1500</field></block>
    </value>
  </block>

  <block type="phaser_time_loop_event">
    <value name="time">
      <block type="math_number"><field name="NUM">1500</field></block>
    </value>
  </block>
{/template}


/**
 * Game blocks.
 */
{template .gameBlocks}
  <block type="phaser_game">
    <next>
      <block type="phaser_game_state">
        <field name="name">main</field>
        <field name="autostart">true</field>
        <statement name="state">
          <block type="phaser_preload">
            <next>
              <block type="phaser_create">
                <next>
                   <block type="phaser_input">
                      <next>
                        <block type="phaser_update">
                          <next>
                            <block type="phaser_render" collapsed="true"></block>
                          </next>
                        </block>
                      </next>
                  </block>
                </next>
              </block>
            </next>
          </block>
        </statement>
      </block>
    </next>
  </block>

  <block type="phaser_game_state">
    <field name="name">game_over</field>
    <field name="autostart">false</field>
    <statement name="state">
      <block type="phaser_preload">
        <next>
          <block type="phaser_create">
            <next>
               <block type="phaser_input">
                  <next>
                    <block type="phaser_update">
                      <next>
                        <block type="phaser_render" collapsed="true"></block>
                      </next>
                    </block>
                  </next>
              </block>
            </next>
          </block>
        </next>
      </block>
    </statement>
  </block>

  <block type="phaser_game_start"></block>

  <block type="phaser_game_start">
    <field name="name">game_over</field>
  </block>

  <block type="phaser_game_restart"></block>
{/template}


/**
 * Sprite blocks.
 */
{template .spriteBlocks}
  {@param? opt_show_create_blocks: bool}

  {if $opt_show_create_blocks}
    <block type="phaser_sprite_add">
      <value name="variable">
        <block type="variables_get">
          <field name="VAR">sprite</field>
        </block>
      </value>
      <value name="x">
        <block type="math_number"><field name="NUM">50</field></block>
      </value>
      <value name="y">
        <block type="math_number"><field name="NUM">50</field></block>
      </value>
    </block>
  {/if}

  <block type="phaser_sprite_adjust">
    <value name="variable">
      <block type="variables_get">
        <field name="VAR">sprite</field>
      </block>
    </value>
    <value name="value">
      <block type="math_number"></block>
    </value>
  </block>

  <block type="phaser_sprite_adjust_dimension">
    <value name="variable">
      <block type="variables_get">
        <field name="VAR">sprite</field>
      </block>
    </value>
    <value name="width">
      <block type="math_number"><field name="NUM">50</field></block>
    </value>
    <value name="height">
      <block type="math_number"><field name="NUM">50</field></block>
    </value>
  </block>

  <block type="phaser_sprite_get">
    <value name="variable">
      <block type="variables_get">
        <field name="VAR">sprite</field>
      </block>
    </value>
  </block>

  <block type="phaser_sprite_destroy">
    <value name="variable">
      <block type="variables_get">
        <field name="VAR">sprite</field>
      </block>
    </value>
  </block>

  <block type="phaser_sprite_immovable">
    <value name="variable">
      <block type="variables_get">
        <field name="VAR">sprite</field>
      </block>
    </value>
  </block>
{/template}
